package cn.broadsense.web.controller;

import cn.broadsense.biz.domain.AppOperLog;
import cn.broadsense.biz.service.AppOperLogService;
import cn.broadsense.biz.service.DataItemService;
import cn.broadsense.common.annotation.Log;
import cn.broadsense.common.core.controller.BaseController;
import cn.broadsense.common.core.domain.R;
import cn.broadsense.common.core.page.TableDataInfo;
import cn.broadsense.common.enums.BusinessType;
import cn.broadsense.common.enums.OperatorType;
import cn.broadsense.common.utils.PageUtils;
import cn.broadsense.common.utils.SecurityUtils;
import cn.broadsense.web.controller.query.LogQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author huangjinshi
 * @description
 * @createDate 2024-09-11 17:47:00
 */
@Api(tags = "日志模块")
@RestController
@RequestMapping("/api/v1/log")
public class LogController extends BaseController {

    private final AppOperLogService appOperLogService;
    private final DataItemService dataItemService;

    @Autowired
    public LogController(AppOperLogService appOperLogService, DataItemService dataItemService) {
        this.appOperLogService = appOperLogService;
        this.dataItemService = dataItemService;
    }

    @GetMapping("/list")
    @ApiOperation("获取日志列表")
    public TableDataInfo getList(LogQuery query) {
        PageUtils.startPage();
        List<AppOperLog> list = appOperLogService
                .getList(
                        AppOperLog.builder()
                                .userId(SecurityUtils.getUserId())
                                .title(query.getTitle())
                                .startTime(query.getStartTime())
                                .endTime(query.getEndTime())
                                .build());
        return getDataTable(list);
    }

    @GetMapping("/{id}")
    @ApiOperation("获取日志详情")
    public R<AppOperLog> getInfo(@PathVariable Long id) {
        AppOperLog appOperLog = appOperLogService.getById(id);
        if (appOperLog == null) {
            return R.fail("当前日志不存在");
        }
        if (!SecurityUtils.getUserId().equals(appOperLog.getUserId())) {
            return R.fail("当前日志不存在");
        }
        return R.ok(appOperLog);
    }

    @DeleteMapping("/{ids}")
    @ApiOperation("删除日志")
    @Log(title = "日志管理", businessType = BusinessType.DELETE, operatorType = OperatorType.MOBILE)
    public R delete(@PathVariable Long[] ids) {
        for (Long id : ids) {
            AppOperLog appOperLog = appOperLogService.getById(id);
            if (appOperLog == null) {
                return R.fail("当前日志不存在");
            }
            if (!SecurityUtils.getUserId().equals(appOperLog.getUserId())) {
                return R.fail("当前日志不存在");
            }
        }
        appOperLogService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }
}
